//
//  GuestureController.m
//  wanzhaoIM
//
//  Created by wanzhao on 15/6/29.
//  Copyright (c) 2015年 liuyuantao. All rights reserved.
//

#import "GuestureController.h"
#import "MicroDef.h"
#import "TextSwitchCell.h"
#import "SettingTextCell.h"
#import "ModifyPasswordVC.h"
#import "CUserSettingModel.h"
#import "CUserSettingMgr.h"
#import "CXmppRequestMgr.h"
#import "ModifyGuesturePasswordController.h"
//#import "CAccountModel.h"
//#import "CAccountMgr.h"
#import "CFolderMgr.h"
#import "CHSAudioPlayer.h"

@interface GuestureController ()<TextSwitchCellDelegate, UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) CUserSettingModel *setting;
@property (nonatomic, strong) CAccountModel *acc;
@property (nonatomic, assign) BOOL is_passWord;
@property (nonatomic, weak) UIView *gustureView;
@property (nonatomic, weak) UITableView *tableView;

@end

@implementation GuestureController

- (void)dealloc
{
    //    [[NSNotificationCenter defaultCenter] removeObserver:self name:K_XMPPSETNOTICESETTING object:nil];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    // Do any additional setup after loading the view.
    
    
    
    //    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveSetting) name:K_XMPPSETNOTICESETTING object:nil];
    
    [self.navigationController setNavigationBarHidden:NO];
    [self.navigationItem setHidesBackButton:YES];
    [self.navigationItem setNewTitleCenter:@"手势锁定密码"];
        
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:tableView];
    self.tableView = tableView;
    self.tableView.dataSource = self;
    self.tableView.frame = CGRectMake(0, 0, HARDWARE_SCREEN_WIDTH, HARDWARE_SCREEN_HEIGHT - 64);
    self.tableView.delegate = self;
    self.tableView.hidden = YES;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.view.backgroundColor = RGBCOLOR(0xf3, 0xf3, 0xf3);

    self.acc = [[CFolderMgr shareInstance].accMgr getRecentlyLoginAccount];
    
    UIView *gustureView = [[UIView alloc] init];
    gustureView.backgroundColor = [UIColor clearColor];
    gustureView.frame = CGRectMake(0, 0, HARDWARE_SCREEN_WIDTH, HARDWARE_SCREEN_HEIGHT - 64);
    gustureView.hidden = YES;
    [self.view addSubview:gustureView];
    self.gustureView = gustureView;
    
    UIButton *imgButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [imgButton setImage:[UIImage imageNamed:@"gesture_guide_pic"] forState:UIControlStateNormal];
    imgButton.userInteractionEnabled = NO;
    imgButton.frame = CGRectMake(0, 15, HARDWARE_SCREEN_WIDTH, 276);
    [gustureView addSubview:imgButton];
    
    UILabel *desLabel = [[UILabel alloc] init];
    desLabel.font = [UIFont systemFontOfSize:14.0];
    desLabel.textColor = [UIColor blackColor];
    desLabel.numberOfLines = 0;
    desLabel.textAlignment = UITextAlignmentCenter;
    desLabel.text = @"你可以创建一个e蜂通信解锁图案，这样他人在借用你的手机时，将无法打开e蜂通信";
    desLabel.frame = CGRectMake(15, CGRectGetMaxY(imgButton.frame) + 15, HARDWARE_SCREEN_WIDTH - 30, 40);
    [gustureView addSubview:desLabel];
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"创建手势密码" forState:UIControlStateNormal];
    [button setBackgroundColor:RGBCOLOR(0x2c, 0xaa, 0xfe)];
    [button addTarget:self action:@selector(creatGuesturePassWord) forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(15, CGRectGetMaxY(desLabel.frame) + 15, HARDWARE_SCREEN_WIDTH - 30, 40);
    [gustureView addSubview:button];
}


- (void)creatGuesturePassWord
{
    ModifyGuesturePasswordController *vc = [[ModifyGuesturePasswordController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];

}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
     NSString *password = [USER_DEFAULT objectForKey:GUESTUREPASSWORD];
    if (password == nil || password.length <= 3) {
        self.tableView.hidden = YES;
        self.gustureView.hidden = NO;
    } else {
        self.tableView.hidden = NO;
        self.gustureView.hidden = YES;
        self.is_passWord = [USER_DEFAULT boolForKey:is_GuseturePassword];
        [self.tableView reloadData];
    }
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */



#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    if (!self.is_passWord) {
        return 1;
    } else {
        return 2;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"TextSwitchCell";
    static NSString *TextCellIdentifier = @"SettingTextCell";
    //    DLog(@"section:%d, rows:%d", indexPath.section, indexPath.row);
    UITableViewCell *cell = nil;
    if(indexPath.section == 0 && indexPath.row == 0)
    {
        TextSwitchCell  *textSwtichCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (textSwtichCell == nil) {
            textSwtichCell = [[[NSBundle mainBundle] loadNibNamed:@"TextSwitchCell" owner:self options:nil] objectAtIndex:0];
        }
        [textSwtichCell.seperatorLine setHidden:NO];
        textSwtichCell.delegate = self;
        textSwtichCell.switchController.on = self.is_passWord;
        textSwtichCell.indexPath = indexPath;
        [textSwtichCell.titleLabel setText:[self titleName:indexPath]];
        cell = textSwtichCell;
    }
    else
    {
        SettingTextCell *textCell = [tableView dequeueReusableCellWithIdentifier:TextCellIdentifier];
        if (textCell == nil) {
            textCell = [[[NSBundle mainBundle] loadNibNamed:@"SettingTextCell" owner:self options:nil] objectAtIndex:0];
        }
        if(indexPath.section == 0 &&  indexPath.row == 1)
        {
            [textCell.seperatorLine setHidden:NO];
        }
        else
        {
            [textCell.seperatorLine setHidden:YES];
        }
        [textCell.titleLabel setText:[self titleName:indexPath]];
        cell = textCell;
    }
    
    return cell;
}

- (void)SwitchCellDidChangeSettingWithIndexPath:(NSIndexPath *)indexPath settingValue:(BOOL)settingValue
{
   
//    if (settingValue == NO) {
    [USER_DEFAULT setBool:settingValue forKey:is_GuseturePassword];
    [USER_DEFAULT synchronize];
//    }
    self.is_passWord = settingValue;
    [self.tableView reloadData];
    
}


//- (void)saveSetting
//{
//    [[[CUserSettingMgr alloc] init] insetUserSetting:self.setting];
//    [self.tableView reloadData];
//}111111


-(NSString *)titleName:(NSIndexPath *)indexPath
{
    NSString *title = nil;
    
    switch (indexPath.section) {
        case 0:
        {
            switch (indexPath.row) {
                case 0:
                    title = @"手势锁定密码";
                    break;
                case 1:
                    title = @"修改手势密码";
                    break;
                default:
                    break;
            }
        }
            break;
        default:
            break;
    }
    return title;
}




#pragma mark - UITableViewDelegate
//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//
//    CGFloat height = 0;
//    switch (section) {
//        case 0:
//            height = 0;
//            break;
//        default:
//            break;
//    }
//    return height;
//}


//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section   // custom view for header. will be adjusted to default or specified header height
//{
//    UIView *headerView = nil;
//    //    if(section == 0)
//    //    {
//    //        return headerView;
//    //    }
//    //    headerView = [[UIView alloc] init];
//    //    headerView.frame = CGRectMake(0, 0, tableView.frame.size.width, KSECTIONHEIGHT);
//    //    headerView.backgroundColor = RGBCOLOR(0xf3, 0xf3, 0xf3);
//    //    UIView *beginLine = [[UIView alloc] init];
//    //    beginLine.frame = CGRectMake(0, 0, tableView.frame.size.width, 0.5);
//    //    [beginLine setBackgroundColor:KSEPERATORLINECOLOR];
//    //    [headerView addSubview:beginLine];
//    //
//    //    UIView *endLine = [[UIView alloc] init];
//    //    endLine.frame = CGRectMake(0, KSECTIONHEIGHT-0.5, tableView.frame.size.width, 0.5);
//    //    [endLine setBackgroundColor:KSEPERATORLINECOLOR];
//    //    [headerView addSubview:endLine];
//
//    return headerView;
//}
//
//- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section   // custom view for footer. will be adjusted to default or specified footer height
//{
//    UIView *headerView = nil;
//    if(section != 0)
//    {
//        return headerView;
//    }
//    headerView = [[UIView alloc] init];
//    headerView.frame = CGRectMake(0, 0, tableView.frame.size.width, 3);
//    headerView.backgroundColor = RGBCOLOR(0xf3, 0xf3, 0xf3);
//    UIView *beginLine = [[UIView alloc] init];
//    beginLine.frame = CGRectMake(0, 0, tableView.frame.size.width, 0.5);
//    [beginLine setBackgroundColor:KSEPERATORLINECOLOR];
//    [headerView addSubview:beginLine];
//
//
//
//    return headerView;
//
//}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = 50.0f;
    
    if(indexPath.row == 0 && indexPath.section == 0)
    {
        height = 50.0f;
    }
    return height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.section) {
        case 0:
        {
            switch (indexPath.row) {
                case 0:
                    break;
                case 1:
                {
                    [self showModifyGuesturePasswordVC];
                }
                    break;
                default:
                    break;
            }
        }
            break;
        default:
            break;
    }
    
    
}


-(void)showModifyGuesturePasswordVC
{
    ModifyGuesturePasswordController *vc = [[ModifyGuesturePasswordController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    
}


@end
